package com.cans.lightning.business.lowcode.service.impl;

import com.cans.lightning.base.mapper.IBaseMapper;
import com.cans.lightning.base.service.impl.BaseServiceImpl;
import com.cans.lightning.business.lowcode.dao.AppMenuDao;
import com.cans.lightning.business.lowcode.dto.AppMenuDto;
import com.cans.lightning.business.lowcode.entity.AppMenu;
import com.cans.lightning.business.lowcode.mapper.AppMenuMapper;
import com.cans.lightning.business.lowcode.service.api.IAppMenuService;
import com.google.common.collect.Lists;
import org.beetl.sql.mapper.BaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 应用信息
 *
 * @author shencan
 * @date 2020/6/14 20:53
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AppMenuServiceImpl extends BaseServiceImpl<AppMenu, AppMenuDto, String> implements IAppMenuService {

    @Resource
    private AppMenuDao appMenuDao;
    @Resource
    private AppMenuMapper AppMenuMapper;


    @Override
    public BaseMapper<AppMenu> getDaoImpl() {
        return appMenuDao;
    }

    @Override
    public IBaseMapper<AppMenu, AppMenuDto, String> getMapperImpl() {
        return AppMenuMapper;
    }


    @Override
    public void saveOrUpdateDto(AppMenuDto AppMenuDto) {
        AppMenu AppMenu = this.toEntity(AppMenuDto);
        super.saveOrUpdate(AppMenu);

    }

    @Override
    public List<AppMenuDto> getAppMenuList(String appId) {
        return AppMenuMapper.toDtos(appMenuDao.createLambdaQuery().andEq(AppMenu::getAppId, appId).asc(AppMenu::getSortId).select());
    }

    @Override
    public void saveOrUpdateDtoList(String appId,List<AppMenuDto> appMenuDtoList) {
        this.refreshSortId(appMenuDtoList);
        List<AppMenuDto> insertList = appMenuDtoList.stream().filter(item -> item.getId() == null).collect(Collectors.toList());
        List<AppMenuDto> updateList = appMenuDtoList.stream().filter(item -> item.getId() != null).collect(Collectors.toList());
        List<String> dbIds = appMenuDao.createLambdaQuery().andEq(AppMenu::getAppId, appId).select(AppMenu::getId).stream().map(AppMenu::getId).collect(Collectors.toList());
        List<String> saveIds = updateList.stream().map(AppMenuDto::getId).collect(Collectors.toList());
        for (String dbId : dbIds) {
            if (!saveIds.contains(dbId)) {
                appMenuDao.deleteById(dbId);
            }
        }
        this.insertDtoList(insertList);
        for (AppMenuDto appMenuDto : updateList) {
            this.saveOrUpdateDto(appMenuDto);
        }
    }

    /**
     * 刷新排序号
     * @param appMenuDtoList
     */
    private void refreshSortId(List<AppMenuDto> appMenuDtoList) {
        int i = 0;
        for (AppMenuDto appMenuDto : appMenuDtoList) {
            appMenuDto.setSortId(i++);
        }
    }

    @Override
    public List<AppMenuDto> getUserAppMenu(String appId) {
        return this.getMapperImpl().toDtos(appMenuDao.createLambdaQuery().andEq(AppMenu::getAppId, appId).asc(AppMenu::getSortId).select());
    }
}
